import sys

from setenv import setLotteryDir
from setenv import getFileDir
from setenv import getFileName
from read_mysql_lottery import get_num_periods
from read_mysql_lottery import get_lottery_num_mysql
from read_mysql_lottery import save_mysql_lottery
from getdlt_m import fetch_dltlotto_numbers_mysql
from getssq_m import fetch_ssqlotto_numbers_mysql


red_num = {}
blue_num = {}
guize = {}

redAll = 0
blueAll = 0
redNum = 0
blueNum = 0
dqnum = []


def get_dict_value_str(dic):
    result = ""
    for i in dic.keys():
        if dic[i]:
            result = result + str(i) + " "
    return result


def csh_guize(lottery_type):
    global guize
    global redAll
    global blueAll
    global redNum
    global blueNum
    global dqnum

    if lottery_type == "ssq":
        guize[0] = "未中奖"
        guize[10] = "未中奖"
        guize[20] = "未中奖"
        guize[30] = "未中奖"
        guize[40] = "五等奖"
        guize[50] = "四等奖"
        guize[60] = "二等奖"
        guize[1]  = "六等奖"
        guize[11] = "六等奖"
        guize[21] = "六等奖"
        guize[31] = "五等奖"
        guize[41] = "四等奖"
        guize[51] = "三等奖"
        guize[61] = "一等奖"
        redAll = 33
        blueAll = 16
        redNum = 6
        blueNum = 1

    if lottery_type == "dlt":
        guize[0] = "未中奖"
        guize[10] = "未中奖"
        guize[20] = "未中奖"
        guize[30] = "九等奖"
        guize[40] = "七等奖"
        guize[50] = "三等奖"
        guize[1] = "未中奖"
        guize[11] = "未中奖"
        guize[21] = "九等奖"
        guize[31] = "八等奖"
        guize[41] = "五等奖"
        guize[51] = "二等奖"
        guize[2] = "九等奖"
        guize[12] = "九等奖"
        guize[22] = "八等奖"
        guize[32] = "六等奖"
        guize[42] = "四等奖"
        guize[52] = "一等奖"
        redAll = 35
        blueAll = 12
        redNum = 5
        blueNum = 2


#
# gm_num 购买的号码（随机号或者固定选号）
# kj_num 开奖号码
# 比较二者，得出中奖情况

def bijiao(gm_num,kj_num):
    global red_num
    global blue_num

    for i in range(1, redAll + 1):
        red_num[i] = False
    for i in range(1, blueAll + 1):
        blue_num[i] = False


    #    print(str(ss_sj))
    for i in range(0, redNum):
        red_num[int(gm_num[i])] = True

    for i in range(redNum, redNum + blueNum):
        blue_num[int(gm_num[i])] = True

    red = 0
    for i in range(0, redNum):
        if red_num[int(kj_num[i])]:
            red += 1
    blue = 0
    for i in range(redNum, redNum + blueNum):
        if blue_num[int(kj_num[i])]:
            blue += 1

    result_num = int(str(red) + str(blue))

    # print("购买号码")
    result = "开奖号码：\n"
    result = result + str(kj_num) + "\n"
    result = result + "购买号码：" + "\n"
    result=result + get_dict_value_str(red_num) + "\n"
    result=result + get_dict_value_str(blue_num) + "\n"
    result = result + "red：" + str(red) + "，blue：" + str(blue) + "。结论：" + guize[result_num] + "\n"
    # print("red：" + str(red) + "，blue：" + str(blue) + "。结论：" + guize[result])
    # print(result)
    return result
#
#
# def hd_ssq():
#     csh_guize("ssq")
#     num_periods = get_num_periods("ssq", 1)
#     sj_num = get_lottery_num_mysql("lottery_sj", num_periods)
#
#     url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo=1&pageSize=1&systemType=PC'
#     lottery_num = get_lottery_num_mysql("lottery_ssq", num_periods)
#     print(lottery_num)
#     if len(lottery_num) == 0:
#         print("mysql无数据")
#         lottery_num = fetch_ssqlotto_numbers_mysql(url, num_periods)
#         if len(lottery_num) == 0:
#             print("num_periods未开奖")
#             return None
#         print(lottery_num)
#
#     if len(sj_num) == 0:
#         print("未获取随机号码")
#     else:
#         bijiao(sj_num, lottery_num)
#
#     gd_num = [4, 13, 21, 25, 27, 29, 4]
#     bijiao(gd_num, lottery_num)
#
# def hd_dlt():
#
#     csh_guize("dlt")
#     num_periods = get_num_periods("dlt",1)
#     sj_num = get_lottery_num_mysql("lottery_sj",num_periods)
#
#     url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=1&isVerify=0&pageNo=1'
#     lottery_num = get_lottery_num_mysql("lottery_dlt",num_periods)
#     print(lottery_num)
#     if len(lottery_num) == 0:
#         print("mysql无数据")
#         lottery_num = fetch_dltlotto_numbers_mysql(url,num_periods)
#         if len(lottery_num) == 0:
#             print("num_periods未开奖")
#         print(lottery_num)
#
#     if len(sj_num) == 0:
#         print("未获取随机号码")
#     else:
#         bijiao(sj_num,lottery_num)
#
#     gd_num = [ 5 ,13 ,17 ,24 ,29 ,3 ,8 ]
#     bijiao(gd_num,lottery_num)
#

def biji_main(lotter_type,tablename):
    csh_guize(lotter_type)
    num_periods = get_num_periods(lotter_type, 1)

    if num_periods == "":
        print("无待查期数")
        return None


    if lotter_type == "dlt":
        url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=1&isVerify=0&pageNo=1'
    if lotter_type == "ssq":
        url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo=1&pageSize=1&systemType=PC'

    lottery_num = get_lottery_num_mysql(tablename, num_periods)
    result = ""
        # print(lottery_num)
    if len(lottery_num) == 0:
        # print("期数：" + num_periods + "----" + lotter_type + "，mysql无数据")
        if lotter_type == "dlt":
            lottery_num = fetch_dltlotto_numbers_mysql(url, num_periods)
        if lotter_type == "ssq":
            lottery_num = fetch_ssqlotto_numbers_mysql(url, num_periods)
        if len(lottery_num) == 0:
            print("期数：" + num_periods + "----" + lotter_type + "，mysql无数据")
            print("期数：" + num_periods + "----" + lotter_type + "，官网未开奖")
            return None
        # print(lottery_num)
    result = "开奖期数："+num_periods+"\n"
    sj_num2 = get_lottery_num_mysql("lottery_sj", num_periods)
    for sj_num in sj_num2:
        if len(sj_num) == 0:
            print("未获取随机号码")
        else:
            if len(lottery_num) == 1:
                result=result+bijiao(sj_num, lottery_num[0])
            else:
                result=result+bijiao(sj_num, lottery_num)
            # sql="update lottery_state a set state = %s,remark = %s where a.num_periods = %s and state = %s"
            # values = (2,result,num_periods,1)
            # save_mysql_lottery(sql,values)

    if lotter_type == "dlt":
        gd_num = [5, 13, 17, 24, 29, 3, 8]
    if lotter_type == "ssq":
        gd_num = [3, 12, 21, 25, 27, 29, 4]
    if len(lottery_num) == 1:
        result=result + bijiao(gd_num, lottery_num[0])
    else:
        result=result + bijiao(gd_num, lottery_num)

    sql = "update lottery_state a set state = %s,remark = %s where a.num_periods = %s and state = %s"
    values = (2, result, num_periods, 1)
    save_mysql_lottery(sql, values)

    print(result)

if __name__ == '__main__':
    # print(len(sys.argv))
    # print(sys.argv[0],sys.argv[1])

     if len(sys.argv) == 0:
         print("python3 bj_m.py dlt/ssq")
         exit(151)
     if sys.argv[1] == "dlt":
        biji_main("dlt","lottery_dlt");
     if sys.argv[1] == "ssq":
         biji_main("ssq","lottery_ssq");